//解决手机页面左右滑动问题
let startX,startY;
document.addEventListener("touchstart",function(e){
    startX = e.touches[0].pageX; 
    startY = e.touches[0].pageY; 
});   
document.addEventListener("touchmove",function(e){
    var moveX = e.touches[0].pageX;
    var moveY = e.touches[0].pageY;
    if(Math.abs(moveX-startX)>Math.abs(moveY-startY)){
        e.preventDefault();
    }
}, {passive: false});
//首页
$(function(){
	//通知
	let $notif = $(".container .notification");
	$notif.click(function(){
		$(location).attr("href","./pages/index_notif.html")
	});
	//中间12个导航
	let $navLi = $(".container .navigation li");
	$navLi.each(function(index){
		$navLi.eq(index).click(function(){
			$(location).attr("href",`./pages/index_nav.html?index=${index}`);
		})
	});
})






//在线商城js部分
$(function(){
	let $listbar = $(".shop .listbar ul li");
	let $popup = $(".shop .popup");
	$listbar.each(function(index){
		//console.log($(this).index())
		$listbar.eq(index).click(function(){
			$popup.css("display","block");
			switch(index){
				case 0:
					$popup.children().html(`
					<li class="active">全部校区</li>
					<li>XK培训校区</li>
					<li>YY培训校区</li>
					<li>YS培训校区</li>
					<li>CR培训校区</li>
					<li>TY培训校区</li>
					<li>ZJ培训校区</li>
					<li>MZ培训校区</li>
					<li>TSET培训校区</li>
					<li>测试体验校区</li>
					`);
					break;
				case 1:
					$popup.children().html(`
					<li class="active">课程</li>
					<li>物品</li>
					`)
					break;
				case 2:
					$popup.children().html(`
					<li class="active">全部分类</li>
					<li>学科课程</li>
					<li>语言课程</li>
					<li>学历提升</li>
					<li>技能提升</li>
					<li>成人培训</li>
					<li>舞蹈课程</li>
					<li>音乐课程</li>
					<li>美术课程</li>
					<li>体育课程</li>
					<li>早教课程</li>
					<li>美妆课程</li>
					<li>康复课程</li>
					<li>测试课程</li>
					<li>一对一</li>
					<li>幼儿律动A1班</li>
					<li>画画1</li>
					<li>画画2</li>
					<li>画画3</li>
					<li>画画4</li>
					<li>美院班</li>
					<li>专注力体验课（6课次）</li>
					<li>艺术</li>
					<li>儿童画</li>
					<li>软笔书法</li>
					<li>硬笔书法</li>
					<li>体验课2次</li>
					<li>钢琴</li>
					<li>全脑</li>
					<li>健身教练</li>
					<li>化妆专业</li>
					<li>素描</li>
					<li>美术</li>
					`);
					break;
				case 3:
					$popup.children().html(`
					<li class="active">全部</li>
					<li>集体班</li>
					<li>一对一</li>
					<li>一对多</li>
					`)
					break;
			};
			//解决分类中字数过多导致的行高问题
			let $li= $(".shop .popup ul li");
			$li.each(function(i){
				if($li.eq(i).height() > $li.eq(0).height() && $li.eq(i).html().length < 7){
					$li.eq(i).css("lineHeight",$li.eq(i).height()+"px")
				}				
			})
			//li的点击事件
			choose($li);
			return false;//阻止document的点击事件
		});
		$popup.parents().click(function(){//解决document的点击在移动端失效问题
			if($popup.css("display")=="block"){
				$popup.css("display","none");
			}				
		})
		function choose($li){
			$li.each(function(j){
				$li.eq(j).click(function(){
					$listbar.eq(index).children("span").html($(this).html());
					$listbar.eq(index).children("span").addClass("active");
				})
			})
		}
	})
	//商品，购物车
	let $reduce = $(".shoplist .list-info .price .number .reduce");
	let $count = $(".shoplist .list-info .price .number .count");
	let $plus = $(".shoplist .list-info .price .number .plus");
	let $cartNum = $(".botnav .cart .cart-num");
	let num = $cartNum.html();
	let flag = true;
	//加号
	$plus.each(function(index){
		$plus.eq(index).click(function(){
			//$(this).count = $count.html();
			let count = $count.eq(index).html();
			if(!flag || ($reduce.eq(index).css("opacity")==0 && $count.eq(index).css("opacity")==0)){
				$reduce.eq(index).css("opacity","1");
				$count.eq(index).css("opacity","1");
			}
			count++;
			$count.eq(index).html(count)
			num++;
			if(num>=1){
				$cartNum.css("opacity","1");
			}
			$cartNum.html(num);
			return false;//阻止冒泡
		})
	});
	//减号
	$reduce.each(function(index){
		$reduce.eq(index).bind("click",function(){
			//$(this).count = $count.html();
			let count = $count.eq(index).html();
			if(count<2){
				$reduce.eq(index).css("opacity","0");
				$count.eq(index).css("opacity","0");
				$reduce.eq(index).unbind("click");
				flag=false;
			}
			count--;
			$count.eq(index).html(count)
			if(num==1){
				$cartNum.css("opacity","0");
			}
			num--;
			$cartNum.html(num);
			return false;
		})
	});
	
	//商品列表高度的处理
	//let $shoplist = $(".shop .shoplist");
	//let $h = $(window).height() - $(".shop .header").height() - $(".shop .search").height() - $(".shop .listbar").height() - $(".shop .botnav").height() -10;
	//$shoplist.height($h);
	//商品详情
	let $goodslist = $(".shop .shoplist ul li");
	$goodslist.each(function(item){
		$goodslist.eq(item).click(function(){
			//跳转到详情页
			//window.open("./shop_details.html")
			$(location).attr("href","./shop_details.html");
			goodsinfo();
		})
	});
	//详情页
	//goodsinfo();
	function goodsinfo(){
		let $span = $(".shop-details .goods-details .class-details .class-total .total-amount .t-num span");
		let $price = $(".shop-details .goods-details .class-details .class-total .total-price .t-price span");
		let $cartNum1 = $(".shop-details .botnav .cart .cart-num");
		let $nowprice = $(".shop-details .goods-details .class-details .class-info .now-price span");
		let $num1 = $cartNum1.html();
		//加号
		$span.eq(2).click(function(){
			let $count1 = $span.eq(1).html();
			$count1++;
			$span.eq(1).html($count1);
			$price.html(($count1*$nowprice.html()).toFixed(2));//保留两位小数
			$num1++;
			$cartNum1.html($num1);
			$cartNum1.css("opacity","1")
			//console.log($count1)
		});
		//减号
		$span.eq(0).click(function(){
			let $count1 = $span.eq(1).html();
			if(!($count1 < 1)){
				$count1--;
				$span.eq(1).html($count1);
				if($num1 == 1)$cartNum1.css("opacity","0");
				$num1--;
				$cartNum1.html($num1);
				$price.html(($count1*$nowprice.html()).toFixed(2));
			}	
		})
	};
	//购物车与结算
	let $cart = $(".shop .botnav .cart");
	let $settle = $(".shop .botnav .settle");
	$cart.click(function(){
		$(location).attr("href","./shop_cart.html?index=cart");
	});
	$settle.click(function(){
		$(location).attr("href","./shop_cart.html?index=settle");
	});
})

		



